<script>
	import * as upgradeApi from '@/api/upgrade.js';
	import store from '@/store';
	export default {
		/**
		 * 全局变量
		 */
		globalData: {

		},

		/**
		 * 初始化完成时触发
		 */
		onLaunch() {
			// 小程序主动更新
			this.updateManager()
			// #ifdef APP-PLUS
			this.updateApp()
			// #endif
		},

		methods: {

			/**
			 * 小程序主动更新
			 */
			updateManager() {
				const updateManager = uni.getUpdateManager();
				updateManager.onCheckForUpdate(res => {
					// 请求完新版本信息的回调
					// console.log(res.hasUpdate)
				})
				updateManager.onUpdateReady(() => {
					uni.showModal({
						title: '更新提示',
						content: '新版本已经准备好，即将重启应用',
						showCancel: false,
						success(res) {
							if (res.confirm) {
								// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
								updateManager.applyUpdate()
							}
						}
					})
				})
				updateManager.onUpdateFailed(() => {
					// 新的版本下载失败
					uni.showModal({
						title: '更新提示',
						content: '新版本下载失败',
						showCancel: false
					})
				})
			},
			updateApp() {
				uni.getSystemInfo({
					success: async function(res) {
						store.commit('SET_ISIOS', res.osName === 'ios')
						switch (res.osName) {
							case 'android':
								const upgradeRes = await upgradeApi.upgrade({
									app_id: '1'
								});
								plus.runtime.getProperty(plus.runtime.appid, (inf) => {
									if (inf.version !== upgradeRes.data.version_code) {
										uni.showModal({
											title: '更新提示',
											content: `有新版本发布,请点击前往下载新版本`,
											showCancel: false,
											success: function(showModalRes) {
												if (showModalRes.confirm) {
													plus.runtime.openURL(upgradeRes.data
														.apk_url);
												}
											}
										})
									}
								});
								break;
							case 'ios':
								const iosUpgradeRes = await upgradeApi.upgrade({
									app_id: '2'
								});
								plus.runtime.getProperty(plus.runtime.appid, (inf) => {
									if (inf.version !== iosUpgradeRes.data.version_code) {
										let appleId = 1639773732 //app的appleId
										uni.showModal({
											title: '更新提示',
											content: '有新版本发布,请点击前往下载新版本',
											success: function(showModalRes) {
												if (showModalRes.confirm) {
													plus.runtime.launchApplication({
														action: `itms-apps://itunes.apple.com/cn/app/id${appleId}?mt=8`
													}, function(e) {
														console.log('Open system default browser failed: ' + e.message);
													});
												}
											}
										})
									}
								});
							default:
								break;
						}
					}
				})
			}

		}

	}
</script>

<style lang="scss">
	/* 引入uView库样式 */
	@import "uview-ui/index.scss";
</style>

<style>
	/* 项目基础样式 */
	@import "./app.scss";

	.uni-app--showlayout+uni-tabbar.uni-tabbar-bottom,
	.uni-app--showlayout+uni-tabbar.uni-tabbar-bottom .uni-tabbar,
	.uni-app--showlayout+uni-tabbar.uni-tabbar-top,
	.uni-app--showlayout+uni-tabbar.uni-tabbar-top .uni-tabbar {
		left: var(--window-left);
		right: var(--window-right);
	}
</style>